using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LockCrypt.Core;
using LockCrypt.DAL;

namespace LockCrypt.Mobile {
    /// <summary>
    /// A form which prompts the user for a password.
    /// </summary>
    public partial class FrmPromptPass : Form {
        /// <summary>
        /// Initializes a new password entry form.
        /// </summary>
        public FrmPromptPass() {
            InitializeComponent();
            itmAbout.Click += (s, e) => {
                using(FrmAbout frmAbout = new FrmAbout()) {
                    frmAbout.ShowDialog();
                }
            };
            itmOptions.Click += (s, e) => { ShowOptions(); };
            itmExit.Click += (s, e) => {
                this.DialogResult = DialogResult.Cancel;
            };
            itmProceed.Click += (s, e) => {
                if(ValidatePass()) {
                    this.DialogResult = DialogResult.OK;
                }
            };
            txtPassword.KeyDown += delegate(object sender, KeyEventArgs e) {
                if((e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) && ValidatePass()) {
                    this.DialogResult = DialogResult.OK;
                }
                                   };
            txtPassword.Focus();
        }

        /// <summary>
        /// Validates the password entered.
        /// </summary>
        /// <returns><c>true</c> if the password is valid, otherwise <c>false</c></returns>
        private bool ValidatePass() {
            bool validPass = false;
            try {
                DalConfigurationManager.Provider.Encryption.EncryptionKey = txtPassword.Text;
                validPass = DalConfigurationManager.Provider.CheckPassword();
                if(!validPass) {
                    MessageBox.Show(I18nUtils.GetString("Errors", "IncorrectPassword"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            } catch(Exception ex) {
                MessageBox.Show(ex.Message, I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            return validPass;
        }
        /// <summary>
        /// Displays a form to change program options.
        /// </summary>
        private void ShowOptions() {
            using(FrmOptions frmOptions = new FrmOptions()) {
                if(frmOptions.ShowDialog() == DialogResult.OK) {
                    
                }
            }
        }
    }
}